﻿#region License

/*
 © 2012 Alex Kazaev
 * FolderDrive project [http://folderdrive.codeplex.com/]
 * Licensed under Ms-PL license [http://www.opensource.org/licenses/MS-PL]
 */

#endregion License

using System.Configuration;

namespace FolderDrive.Configuration
{
  /// <summary>
  /// Represents the mapping elements collection
  /// </summary>
  [ConfigurationCollection(typeof(FolderDriveConfigurationElement), AddItemName = "mapping", CollectionType = ConfigurationElementCollectionType.BasicMap)]
  internal class FolderDriveConfigurationElementCollection : ConfigurationElementCollection
  {
    /// <summary>
    /// When overridden in a derived class, creates a new <see cref="T:System.Configuration.ConfigurationElement"/>.
    /// </summary>
    /// <returns>
    /// A new <see cref="T:System.Configuration.ConfigurationElement"/>.
    /// </returns>
    protected override ConfigurationElement CreateNewElement()
    {
      return new FolderDriveConfigurationElement();
    }

    /// <summary>
    /// Gets the element key for a specified configuration element when overridden in a derived class.
    /// </summary>
    /// <param name="element">The <see cref="T:System.Configuration.ConfigurationElement"/> to return the key for.</param>
    /// <returns>
    /// An <see cref="T:System.Object"/> that acts as the key for the specified <see cref="T:System.Configuration.ConfigurationElement"/>.
    /// </returns>
    protected override object GetElementKey(ConfigurationElement element)
    {
      return ((FolderDriveConfigurationElement)element).DriveLetter;
    }

    /// <summary>
    /// Adds the specified element.
    /// </summary>
    /// <param name="element">The element.</param>
    public void Add(FolderDriveConfigurationElement element)
    {
      BaseAdd(element);
    }

    /// <summary>
    /// Clears this instance.
    /// </summary>
    public void Clear()
    {
      BaseClear();
    }

    /// <summary>
    /// Gets the index of the given item.
    /// </summary>
    /// <param name="element">The element.</param>
    /// <returns>the index of the given item</returns>
    public int IndexOf(FolderDriveConfigurationElement element)
    {
      return BaseIndexOf(element);
    }

    /// <summary>
    /// Removes the specified element.
    /// </summary>
    /// <param name="element">The element.</param>
    public void Remove(FolderDriveConfigurationElement element)
    {
      if (BaseIndexOf(element) >= 0)
      {
        BaseRemove(element.DriveLetter);
      }
    }

    /// <summary>
    /// Removes the element specified by index.
    /// </summary>
    /// <param name="index">The index.</param>
    public void RemoveAt(int index)
    {
      BaseRemoveAt(index);
    }

    /// <summary>
    /// Gets or sets a property, attribute, or child element of this configuration element.
    /// </summary>
    /// <returns>The specified property, attribute, or child element</returns>
    public FolderDriveConfigurationElement this[int index]
    {
      get { return (FolderDriveConfigurationElement)BaseGet(index); }
      set
      {
        if (BaseGet(index) != null)
        {
          BaseRemoveAt(index);
        }
        BaseAdd(index, value);
      }
    }
  }
}